<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>div单体模式</title>
</head>
<body>
	<h1>div单体模式</h1>
</body>
<script type="text/javascript">

    // 单体模式
    var CreateDiv = function(html) {
        this.html = html;
        this.init();
    }
    CreateDiv.prototype.init = function(){
        var div = document.createElement("div");
        div.innerHTML = this.html;
        document.body.appendChild(div);
    };
    // 代理实现单体模式
    var ProxyMode = (function(){
        var instance;
        return function(html) {
            if(!instance) {
                instance = new CreateDiv(html);
            }
            return instance;
        } 
    })();
    var a = new ProxyMode("aaa");
    var b = new ProxyMode("bbb");
    console.log(a===b);// true
    // 只会创建一个div

</script>
</html>